package com.lpy.book.controller;


import com.lpy.book.constonts.Constonts;
import com.lpy.book.model.UserInfo;
import com.lpy.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public boolean login(String userName, String password, HttpSession session) {

        // 检验参数
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }

        // 得到用户信息
        UserInfo userInfo = userService.queryByName(userName);
        if (userInfo == null) {
            return false;
        }

        // 判断输入的是否和数据库查询的密码一致
        if (password.equals(userInfo.getPassword())) {

            userInfo.setPassword("");

            session.setAttribute(Constonts.USER_SESSION_KEY, userInfo);

            return true;
        }

        return false;
    }

}
